﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class UdfReportSettingDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbIsMustInput;
        protected DropDownList ddlCtrlType;
        protected DropDownList ddlSysCode;
        protected HtmlForm F1873;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdntype;
        protected Label lblCtrlID;
        protected Label lblCtrlLable;
        protected Label lblCtrlType;
        protected Label lblDefaultValue;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblParamName;
        protected Label lblReportID;
        protected Label lblUdf1;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblUdfReportSetting;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCtrlID;
        protected TextBox txtCtrlLable;
        protected TextBox txtDefaultValue;
        protected TextBox txtLineID;
        protected TextBox txtParamName;
        protected TextBox txtReportID;
        protected TextBox txtUdf1;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("UdfReportSetting.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtReportID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                if (string.IsNullOrEmpty(this.txtCtrlID.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_005");
                    this.txtCtrlID.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCtrlLable.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_006");
                    this.txtCtrlLable.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtParamName.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_007");
                    this.txtParamName.Focus();
                    return;
                }
                UdfReportSettingDetailInfo udfReportSettingDetailInfo = new UdfReportSettingDetailInfo {
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    ReportID = this.txtReportID.Text.Trim(),
                    CtrlID = this.txtCtrlID.Text.Trim(),
                    CtrlLable = this.txtCtrlLable.Text.Trim(),
                    CtrlType = this.ddlCtrlType.Text.Trim()
                };
                if (string.Compare(this.ddlCtrlType.Text, "DROPDOWNLIST", true) == 0)
                {
                    udfReportSettingDetailInfo.DefaultValue = this.ddlSysCode.SelectedValue;
                }
                else
                {
                    udfReportSettingDetailInfo.DefaultValue = this.txtDefaultValue.Text.Trim();
                }
                udfReportSettingDetailInfo.ParamName = this.txtParamName.Text.Trim();
                udfReportSettingDetailInfo.IsMustInput = this.chbIsMustInput.Checked ? "Y" : "N";
                udfReportSettingDetailInfo.Udf1 = this.txtUdf1.Text.Trim();
                udfReportSettingDetailInfo.Udf2 = this.txtUdf2.Text.Trim();
                udfReportSettingDetailInfo.Udf3 = this.txtUdf3.Text.Trim();
                udfReportSettingDetailInfo.Udf4 = this.txtUdf4.Text.Trim();
                udfReportSettingDetailInfo.Udf5 = this.txtUdf5.Text.Trim();
                udfReportSettingDetailInfo.CreatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(this.hdntype.Value) && (this.hdntype.Value == "edit"))
                {
                    udfReportSettingDetailInfo.UpdatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(udfReportSettingDetailInfo, "UDF_REPORT_SETTING_DETAIL")))
                    {
                        success = true;
                    }
                }
                else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(udfReportSettingDetailInfo, "UDF_REPORT_SETTING_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1870_004") + ex.Message;
                return;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("UdfReportSetting.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtReportID.Text.Trim())));
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                if (string.IsNullOrEmpty(this.txtCtrlID.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_005");
                    this.txtCtrlID.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCtrlLable.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_006");
                    this.txtCtrlLable.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtParamName.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1870_007");
                    this.txtParamName.Focus();
                    return;
                }
                UdfReportSettingDetailInfo udfReportSettingDetailInfo = new UdfReportSettingDetailInfo {
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    ReportID = this.txtReportID.Text.Trim(),
                    CtrlID = this.txtCtrlID.Text.Trim(),
                    CtrlLable = this.txtCtrlLable.Text.Trim(),
                    CtrlType = this.ddlCtrlType.Text.Trim()
                };
                if (string.Compare(this.ddlCtrlType.Text, "DROPDOWNLIST", true) == 0)
                {
                    udfReportSettingDetailInfo.DefaultValue = this.ddlSysCode.SelectedValue;
                }
                else
                {
                    udfReportSettingDetailInfo.DefaultValue = this.txtDefaultValue.Text.Trim();
                }
                udfReportSettingDetailInfo.ParamName = this.txtParamName.Text.Trim();
                udfReportSettingDetailInfo.IsMustInput = this.chbIsMustInput.Checked ? "Y" : "N";
                udfReportSettingDetailInfo.Udf1 = this.txtUdf1.Text.Trim();
                udfReportSettingDetailInfo.Udf2 = this.txtUdf2.Text.Trim();
                udfReportSettingDetailInfo.Udf3 = this.txtUdf3.Text.Trim();
                udfReportSettingDetailInfo.Udf4 = this.txtUdf4.Text.Trim();
                udfReportSettingDetailInfo.Udf5 = this.txtUdf5.Text.Trim();
                udfReportSettingDetailInfo.CreatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(udfReportSettingDetailInfo, "UDF_REPORT_SETTING_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1870_004") + ex.Message;
                return;
            }
            if (success)
            {
                this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                this.txtCtrlID.Text = "";
                this.txtCtrlLable.Text = "";
                this.ddlCtrlType.SelectedIndex = 0;
                this.txtDefaultValue.Text = "";
                this.ddlSysCode.SelectedIndex = 0;
                this.txtParamName.Text = "";
                this.chbIsMustInput.Checked = false;
                this.txtUdf1.Text = "";
                this.txtUdf2.Text = "";
                this.txtUdf3.Text = "";
                this.txtUdf4.Text = "";
                this.txtUdf5.Text = "";
            }
        }

        private void LoadData()
        {
            this.ddlCtrlType.Items.Clear();
            this.ddlCtrlType.Items.Add("TEXTBOX");
            this.ddlCtrlType.Items.Add("DROPDOWNLIST");
            this.ddlCtrlType.Items.Add("DATETIME");
            List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> syscode = CommoneMethodServiceClientProxy.GetInstance().GetSysCode4DDL();
            this.ddlSysCode.DataSource = syscode;
            this.ddlSysCode.DataTextField = "SysCodeText";
            this.ddlSysCode.DataValueField = "SysCodeValue";
            this.ddlSysCode.DataBind();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                this.txtReportID.Text = base.Request.QueryString["ID"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                this.txtLineID.Text = base.Request.QueryString["SeqNO"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]) && (base.Request.QueryString["type"].ToString() == "edit"))
            {
                this.btnSaveContinue.Enabled = false;
                this.hdntype.Value = base.Request.QueryString["type"].ToString();
                this.SetUdfReportSettingDetail2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetUdfReportSettingDetail2Form()
        {
            if ((this.txtReportID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                UdfReportSettingDetailInfo udfReportSettingDetailInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtReportID.Text.Trim(), this.txtLineID.Text.Trim(), "", "UDF_REPORT_SETTING_DETAIL") as UdfReportSettingDetailInfo;
                if (udfReportSettingDetailInfo != null)
                {
                    this.txtLineID.Text = udfReportSettingDetailInfo.LineID.ToString();
                    this.txtReportID.Text = udfReportSettingDetailInfo.ReportID;
                    this.txtCtrlID.Text = udfReportSettingDetailInfo.CtrlID;
                    this.txtCtrlLable.Text = udfReportSettingDetailInfo.CtrlLable;
                    this.ddlCtrlType.Text = udfReportSettingDetailInfo.CtrlType;
                    if (string.Compare(this.ddlCtrlType.Text, "DROPDOWNLIST", true) == 0)
                    {
                        this.ddlSysCode.SelectedValue = udfReportSettingDetailInfo.DefaultValue;
                        this.ddlSysCode.Style.Add("display", "block");
                        this.txtDefaultValue.Style.Add("display", "none");
                    }
                    else
                    {
                        this.txtDefaultValue.Text = udfReportSettingDetailInfo.DefaultValue;
                        this.ddlSysCode.Style.Add("display", "none");
                        this.txtDefaultValue.Style.Add("display", "block");
                    }
                    this.txtParamName.Text = udfReportSettingDetailInfo.ParamName;
                    this.chbIsMustInput.Checked = udfReportSettingDetailInfo.IsMustInput == "Y";
                    this.txtUdf1.Text = udfReportSettingDetailInfo.Udf1;
                    this.txtUdf2.Text = udfReportSettingDetailInfo.Udf2;
                    this.txtUdf3.Text = udfReportSettingDetailInfo.Udf3;
                    this.txtUdf4.Text = udfReportSettingDetailInfo.Udf4;
                    this.txtUdf5.Text = udfReportSettingDetailInfo.Udf5;
                }
            }
        }
    }
}

